<template>
  <h1>员工列表练习</h1>
  <input type="text" placeholder="请输入员工姓名" v-model="user.name">
  <input type="text" placeholder="请输入员工工资" v-model="user.salary">
  <input type="text" placeholder="请输入员工岗位" v-model="user.post">
  <button @click="add">点我添加</button>
  <hr>
  <table border="1px">
    <tr>
      <th>员工编号</th>
      <th>员工姓名</th>
      <th>员工工资</th>
      <th>员工岗位</th>
    </tr>
    <tr v-for="(u,index) in arr" >
      <td>{{index+1}}</td>
      <td>{{u.name}}</td>
      <td>{{u.salary}}</td>
      <td>{{u.post}}</td>
    </tr>
  </table>
</template>

<script setup>
  import {ref} from "vue";
//定义响应式对象来保存收集到的员工信息
  const user=ref({
    name:'',
    salary:'',
    post:''
  });
  //定义数组来保存多个员工信息
  const arr=ref([]);
  //定义添加员工的方法
  const add=()=>{
    if(user.value.name.trim()==''||user.value.salary.trim()==''||user.value.post.trim()==''){
      alert('请填写完整信息');
      return;
    }
    //将收集到的员工数据存储到数组中
    arr.value.push(user.value);
    user.value={
      name:'',
      salary:'',
      post:''
    };
    console.log(arr.value);
  }

</script>

<style scoped>

</style>